// pages/medical/components/schedule/index.js
import {
  hospitalApi
} from "../../../../utils/hospital-api";
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item: {
      type: Object
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    finnalItem: {},
    show: false,
    schState: {
      0: '立即预约',
      1: '已过期',
      2: '停诊',
      3: '已满',
    },
    hospital: {}
  },
  lifetimes: {
    // 在组件实例进入页面节点树时执行
    attached: function () {
      this.setData({
        hospital: wx.getStorageSync('hospital'),
      })

      if (this.data.item.RESERVETYPE == 2) {
        let item = JSON.parse(JSON.stringify(this.data.item))
        hospitalApi({
          method: 'getDoctor',
          data: JSON.stringify({
            hosCode: item.HOSCODE,
            depId: item.DEPID,
            docId: item.DOCID,
          })
        }).then(res => {
          item.docInfo = res.data[0]
          this.setData({
            finnalItem: item
          })
        })
      } else {
        this.setData({
          finnalItem: this.data.item
        })
      }
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onLoad: function () {

      // 页面参数 paramA 的值
    },
    getDoctorInfo(item) {
      return new Promise((resolve, reject) => {

      })
    },
    // 跳转到医生排版的页面
    toPage() {
      const item = this.data.finnalItem.docInfo;
      if (item.DOCID) {
        wx.setStorageSync('doctor', item); //保存选中的医生信息
        wx.navigateTo({
          url: `/pages/medical/schedule/index?deptId=${this.data.item.DEPID}&hosCode=${this.data.item.HOSCODE}&docId=${item.DOCID}&activeDate=${wx.getStorageSync('activeDate')}&dateFull=${wx.getStorageSync('dateFull')}`
        })
      } else {
        wx.showToast({
          title: '医生代码错误！',
          icon: 'error'
        })
      }
    },
    showPopup() {
      this.setData({
        show: true
      });
    },
    onClose() {
      this.setData({
        show: false
      });
    },
    // 跳转预约
    reservation(e) {
      const parent = this.data.item;
      wx.setStorageSync('schedule', parent); //保存选中的排班信息
      let url;
      if (this.data.hospital.ISSEG == 1) {
        this.setData({
          show: true
        });
        const item = e.mark.item;
        wx.setStorageSync('scheduleTime', item); //保存选中的时间信息
        url = `/pages/medical/reservation/index?schCode=${this.data.item.SCHCODE}&secCode=${item.SECCODE}`;
      } else {
        url = `/pages/medical/reservation/index?schCode=${this.data.item.SCHCODE}`;
      }
      const user = wx.getStorageSync('userInfo')
      if (user != '') {
        wx.navigateTo({
          url: url
        })
      } else {
        hospitalApi({
          method: 'getPatientByIdnum',
          data: JSON.stringify({
            idnum: user.idNumber
          })
        }).then(res => {
          if (res.code == 1000) {
            // wx.setStorageSync('user', res.data)
            wx.navigateTo({
              url: url
            })
          }
        })
      }
    },
  }
})